package com.qiyi.player.core.ad.file
{
   import flash.utils.ByteArray;
   import com.qiyi.player.base.flv.FLVParser;
   import com.qiyi.player.core.video.file.actors.datatile.TagInfo;
   import com.qiyi.player.base.flv.FLVTagScriptDataObject;
   import com.qiyi.player.base.flv.FLVTag;
   import com.qiyi.player.base.logging.ILogger;
   import com.qiyi.player.base.flv.FLVTagAudio;
   import com.qiyi.player.base.flv.FLVTagVideo;
   import com.qiyi.player.base.logging.Log;
   
   public class AdFlv extends Object
   {
      
      public function AdFlv() {
         this._log = Log.getLogger("com.qiyi.player.core.ad.AdFlv");
         super();
         this._data = new ByteArray();
         this._parser = new FLVParser(true);
      }
      
      private var _data:ByteArray;
      
      private var _imported:Boolean;
      
      private var _exported:Boolean;
      
      private var _isLastAd:Boolean;
      
      private var _currentPos:int = 0;
      
      private var _header:ByteArray;
      
      private var _parser:FLVParser;
      
      private var _videoTags:Vector.<TagInfo>;
      
      private var _metadata:FLVTagScriptDataObject = null;
      
      private var _configVideoTag:FLVTag = null;
      
      private var _configAudioTag:FLVTag = null;
      
      private var _startTime:int = -1;
      
      private var _duration:int;
      
      private var _id:String = "";
      
      protected var _log:ILogger;
      
      public function get id() : String {
         return this._id;
      }
      
      public function get exported() : Boolean {
         return this._exported;
      }
      
      public function set exported(param1:Boolean) : void {
         this._exported = param1;
      }
      
      public function get imported() : Boolean {
         return this._imported;
      }
      
      public function set imported(param1:Boolean) : void {
         if(param1 != this._imported)
         {
            this._imported = param1;
         }
      }
      
      public function set isLastAd(param1:Boolean) : void {
         this._isLastAd = param1;
      }
      
      public function get isLastAd() : Boolean {
         return this._isLastAd;
      }
      
      public function get duration() : int {
         return this._duration;
      }
      
      public function get size() : int {
         return this._data.length;
      }
      
      public function get currentPos() : int {
         return this._currentPos;
      }
      
      public function destroy() : void {
         this._id = "";
         this._parser = null;
         this._data.clear();
         this._data = null;
         this._videoTags = null;
         this._header = null;
         this._metadata = null;
         this._configVideoTag = null;
         this._configAudioTag = null;
      }
      
      public function inject(param1:String, param2:ByteArray, param3:Boolean) : void {
         if(param1 != this._id)
         {
            this._id = param1;
         }
         if(param2)
         {
            this.tryParseData(param2);
         }
         this._isLastAd = param3;
      }
      
      public function read(param1:int = -1, param2:int = -1) : ByteArray {
         var _loc3_:ByteArray = null;
         if(!this._exported && this.size > 0)
         {
            _loc3_ = new ByteArray();
            if(param1 < 0)
            {
               param1 = this._currentPos;
            }
            if(param2 < 0)
            {
               param2 = this._data.length - this._currentPos;
            }
            if(param2 > 0)
            {
               _loc3_.writeBytes(this._data,param1,param2);
               this._currentPos = param1 + param2;
               if(this._currentPos >= this._data.length && (this._imported))
               {
                  this._exported = true;
               }
               return _loc3_;
            }
            this._log.debug("[AdFlv.read] error: read flv data our of range, id = " + this._id);
         }
         return null;
      }
      
      public function get header() : ByteArray {
         return this._header;
      }
      
      private function tryParseData(param1:ByteArray) : void {
         var var_19:ByteArray = param1;
         if((var_19.length) && (this._parser))
         {
            try
            {
               this._parser.parse(var_19,true,this.onEachFLVTag);
               if(this._imported)
               {
                  this._parser = null;
               }
            }
            catch(e:Error)
            {
               _log.error("ad FLV Parser Error");
            }
         }
         if((var_19.length) && (this._parser))
         {
            return;
         }
      }
      
      private function onEachFLVTag(param1:FLVTag) : Boolean {
         var _loc4_:TagInfo = null;
         var _loc2_:* = this._data.length == 0;
         if(_loc2_)
         {
            this._parser.getHeader().write(this._data);
            this._header = new ByteArray();
            this._header.writeBytes(this._data,0,this._data.length);
            this._currentPos = this._header.length;
         }
         if(param1.tagType == FLVTag.TAG_TYPE_SCRIPTDATAOBJECT)
         {
            if(this._metadata == null && (_loc2_))
            {
               this._metadata = param1 as FLVTagScriptDataObject;
            }
            param1.write(this._data);
            return true;
         }
         if(param1.tagType == FLVTag.TAG_TYPE_AUDIO)
         {
            if(FLVTagAudio(param1).isAACSequenceHeader)
            {
               if(this._configAudioTag == null)
               {
                  this._configAudioTag = param1;
                  param1.write(this._data);
               }
               return true;
            }
         }
         else if(param1.tagType == FLVTag.TAG_TYPE_VIDEO)
         {
            if(FLVTagVideo(param1).avcPacketType == 0)
            {
               if(this._configVideoTag == null)
               {
                  this._configVideoTag = param1;
                  param1.write(this._data);
               }
               return true;
            }
         }
         
         var _loc3_:uint = param1.timestamp;
         if(param1.tagType == FLVTag.TAG_TYPE_VIDEO && FLVTagVideo(param1).frameType == FLVTagVideo.FRAME_TYPE_KEYFRAME)
         {
            if(!this._videoTags)
            {
               this._videoTags = new Vector.<TagInfo>();
            }
            _loc4_ = new TagInfo();
            _loc4_.keyframe = true;
            _loc4_.position = this._data.length;
            _loc4_.time = param1.timestamp;
            this._videoTags.push(_loc4_);
            if(this._startTime == -1)
            {
               this._startTime = _loc3_;
            }
         }
         if(this._startTime != -1)
         {
            this._duration = _loc3_ - this._startTime;
         }
         param1.write(this._data);
         return true;
      }
   }
}
